#include <QDialogButtonBox>
#include <QMessageBox>
#include <QPushButton>

#include "networksettingsdialog.h"
#include "ui_networksettingsdialog.h"

#include "settings.h"

#include "rcsession.h"

#define SIGN_IN   "Sign In"
#define SIGN_OUT  "Sign Out"
#define OK        "Ok"

NetworkSettingsDialog::NetworkSettingsDialog(RcSession * rcSession,
                                             QWidget   * parent) :
    QDialog(parent),
    ui(new Ui::NetworkSettingsDialog),
    m_rcSession(rcSession),
    m_justSignedIn(false)
{
    ui->setupUi(this);

    ui->m_authUserName->setText(Settings::get(Settings::ForumNokiaUserName).toString());

    switch (Settings::get(Settings::ProxyConfig).toInt())
    {
    case Settings::PCK_SystemProxy:
        ui->m_systemProxy->setChecked(true);
        setEnableManualProxyWidgets(false);
        break;
    case Settings::PCK_ManualProxy:
        ui->m_manualProxy->setChecked(true);
        setEnableManualProxyWidgets(true);
        ui->m_proxyUrl->setText(Settings::get(Settings::ProxyUrl).toString());
        ui->m_proxyPort->setValue(Settings::get(Settings::ProxyPort).toInt());
        ui->m_proxyUserName->setText(Settings::get(Settings::ProxyUserName).toString());
        ui->m_proxyPassword->setText(Settings::get(Settings::ProxyPassword).toString());
        break;
    case Settings::PCK_UndefinedProxyConfig:
    case Settings::PCK_NoProxy:
    default:
        ui->m_noProxy->setChecked(true);
        setEnableManualProxyWidgets(false);
        break;
    }

    updateSignedOnAppearance();

    connect(ui->m_manualProxy,
            SIGNAL(toggled(bool)),
            this,
            SLOT(on_manualProxy_toggled(bool)));

    if (m_rcSession != NULL)
    {
        connect(m_rcSession,
                SIGNAL(signInCompleted(bool)),
                this,
                SLOT(rcSessionSignedInSlot(bool)));
    }
}


NetworkSettingsDialog::~NetworkSettingsDialog()
{
    if (m_rcSession != NULL)
    {
        disconnect(m_rcSession,
                   SIGNAL(refreshRcPropertiesCompleted(bool)),
                   this,
                   SLOT(rcSessionSignedInSlot(bool)));
    }

    delete ui;
}


bool NetworkSettingsDialog::justSignedIn() const
{
    return m_justSignedIn;
}


void NetworkSettingsDialog::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}


void NetworkSettingsDialog::accept()
{
    if (m_rcSession == NULL)
    {
        // merely for persisting settings
        persistSettings();
        done(0);
    }
    else
    {
        bool
            signedOn = m_rcSession->isSignedOn();

        if (signedOn)
        {
            ui->buttonBox->setEnabled(false);
            m_rcSession->signOut();
            updateSignedOnAppearance();
            this->m_justSignedIn = false;
            ui->buttonBox->setEnabled(true);

            done(0);
        }
        else
        {
            ui->buttonBox->setEnabled(false);
            persistSettings();
            m_rcSession->signIn();
        }
    }
}


void NetworkSettingsDialog::on_manualProxy_toggled(bool toggle)
{
    setEnableManualProxyWidgets(toggle);
}


void NetworkSettingsDialog::rcSessionSignedInSlot(bool success)
{
    updateSignedOnAppearance();
    m_justSignedIn = success;
    ui->buttonBox->setEnabled(true);

    if (!success)
    {
        QMessageBox::warning(this,
                             "Remote compiler connection failed",
                             "Signing in failed. Check your Forum Nokia account/password,\n"
                             "your proxy settings and the availability of SSL library",
                             QMessageBox::Ok);
    }
    else
    {
        done(0);
    }
}



void NetworkSettingsDialog::setEnableManualProxyWidgets(bool enabled)
{
    ui->m_proxyUrl->setEnabled(enabled);
    ui->m_proxyPort->setEnabled(enabled);
    ui->m_proxyUserName->setEnabled(enabled);
    ui->m_proxyPassword->setEnabled(enabled);
}


void NetworkSettingsDialog::persistSettings()
{
    Settings::set(Settings::ForumNokiaUserName,
                  ui->m_authUserName->text());

    Settings::ProxyConfigKind
        proxyConfig = Settings::PCK_NoProxy;

    if (ui->m_systemProxy->isChecked())
    {
        proxyConfig = Settings::PCK_SystemProxy;
    }
    else if (ui->m_manualProxy->isChecked())
    {
        proxyConfig = Settings::PCK_ManualProxy;
        Settings::set(Settings::ProxyUrl,
                      ui->m_proxyUrl->text());
        Settings::set(Settings::ProxyPort,
                      ui->m_proxyPort->value());
        Settings::set(Settings::ProxyUserName,
                      ui->m_proxyUserName->text());
        Settings::set(Settings::ProxyPassword,
                      ui->m_proxyPassword->text());
    }

    Settings::set(Settings::ProxyConfig,
                  proxyConfig);
}


void NetworkSettingsDialog::updateSignedOnAppearance()
{
    if (m_rcSession)
    {
        bool
            signedOn = m_rcSession->isSignedOn();
        ui->buttonBox->button(QDialogButtonBox::Ok)->setText(signedOn ? SIGN_OUT : SIGN_IN);
        ui->m_fNGroupBox->setEnabled(!signedOn);
        ui->m_pSGroupBox->setEnabled(!signedOn);
    }
    else
    {
        ui->buttonBox->button(QDialogButtonBox::Ok)->setText(OK);
    }
}
